'''
Created on Dec 7, 2009

@author: mkiyer
'''

import veggie.alignment.sam as sam
import os
import sys
import logging
import subprocess

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    insamfile = sys.argv[1]
    outsamfile = sys.argv[2]
    import veggie.genome.chrom as genome    
    references = genome.HG18.chrom_sizes.keys()

    filterbamfile = os.path.splitext(outsamfile)[0] + '.filter.bam'    
    logging.debug("Filtering BAM file %s" % (filterbamfile))
    sam.filter_reads(insamfile, filterbamfile,
                     unique_only=False,
                     references=references)
    # sort and index
    logging.debug("Sorting BAM file %s" % (filterbamfile))
    subprocess.call(['samtools', 'sort', filterbamfile, os.path.splitext(outsamfile)[0]])
    logging.debug("Indexing BAM file %s" % (outsamfile))
    subprocess.call(['samtools', 'index', outsamfile])
    